/**
 * 奖品表 列表页
 *
 * 作者 admin
 * 日期 2024-08-06 09:41:04
 */
layui.use(['jquery', 'laydate', 'febs', 'form', 'table', 'commonJS', 'validate'], function () {
    var $ = layui.jquery,
        laydate = layui.laydate,
        febs = layui.febs,
        form = layui.form,
        table = layui.table,
        commonJS = layui.commonJS,
        validate = layui.validate,
        $view = $('#febs-baseGoods'),
        $query = $view.find('#query'),
        $add = $view.find('#add'),
        $searchForm = $view.find('#baseGoods-table-form'),
        tableIns;

    form.verify(validate);
    form.render();

    initTable();

    function initTable() {
        tableIns = febs.table.init({
            elem: $view.find('table'),
            id: 'baseGoodsTable',
            url: ctx + 'baseGoods/list',
            where: getQueryParams(),
            height: getTableHeight(),
            skin: '',
            cols: [[
                {type: 'numbers', title: '序号'},
                {
                    field: 'smallPath', title: '奖品图片', width: 120, align: 'center', templet: function (d) {
                        var img = '<img class="tupian" style="width: 60px;height: 60px;" src="' + ctx + d.smallPath + '" ' +
                            'data-bigsrc="' + ctx + d.filePath + '"  lay-event="imgEvent" /></img>';
                        return img;
                    }
                },
                {
                    field: 'goodsName',
                    title: '奖品名称',
                    minWidth: 120,
                    style: 'color:#01aaed;cursor:pointer',
                    event: 'detail'
                },

                {field: 'goodsPrice', title: '单价', width: 120, align: 'center'},
                {field: 'goodsUnit', title: '单位', width: 120, align: 'center'},
                {field: 'exchangeScore', title: '兑换所需积分', width: 120, align: 'center'},
                {field: 'inventory', title: '在库数量', width: 120, align: 'center'},
                {field: 'lockNumber', title: '锁定数量', width: 120, align: 'center'},
                {field: 'status', title: '状态', toolbar: '#statusOption', align: 'center', width: 120},
                {title: '操作', toolbar: '#option', align: 'center', width: 120}
            ]],
            toolbar: '#baseGoods-toolbar',
            defaultToolbar: []
        });
        // tableIns.reload({url: ctx + 'baseGoods/list', where: getQueryParams(), page: {curr: 1}});
    }

    table.on('sort(baseGoodsTable)', function (obj) {
        tableIns.reload({
            initSort: obj,
            where: $.extend(getQueryParams(), {
                field: obj.field,
                order: obj.type
            }),
            autoSort: true
        });
    });

    function reloadPage(curr) {
        var _curr = $view.find(".layui-laypage-em").next().html();
        if (!!curr) {
            _curr = curr;
        }
        tableIns.reload({where: getQueryParams(), page: {curr: _curr}});
    }

    $query.on('click', function () {
        reloadPage(1);
    });

    table.on('toolbar(baseGoodsTable)', function (obj) {
        if (obj.event === 'add') {
            febs.modal.open('新增奖品', 'baseGoods/add', {
                btn: ['提交','取消'],
                area: $(window).width() <= 750 ? '90%' : ['700px','600px'],
                yes: function (index, layero) {
                    $('#baseGoods-edit').find('#submit').trigger('click');
                },
                data: {
                    tenantId: $searchForm.find('select[name="tenantId"]').val(),
                    tenantName: $searchForm.find('select[name="tenantId"]').find('option:selected').text().trim()
                }
            });
        }
    });

    table.on('tool(baseGoodsTable)', function (obj) {
        //表格编辑删除事件
        var id = obj.data.goodsId;
        var layEvent = obj.event;
        if (layEvent === 'edit') {
            febs.modal.open('修改奖品', 'baseGoods/update/' + id, {
                btn: ['提交', '取消'],
                area: $(window).width() <= 750 ? '90%' : ['700px','600px'],
                yes: function (index, layero) {
                    $('#baseGoods-edit').find('#submit').trigger('click');
                },
                btn2: function () {
                    layer.closeAll();
                },
            });
        } else if (layEvent === 'del') {
            febs.modal.confirm('删除奖品', '确定删除所选奖品？', function () {
                deleteIds(id);
            });
        } else if (layEvent === 'imgEvent') {
            $view.find(".big-img-div img").attr("src", ctx + obj.data.filePath);
        } else if (layEvent === 'detail') {
            febs.modal.open('奖品详情', 'baseGoods/detail/' + id, {
                area: $(window).width() <= 750 ? '90%' : ['700px','600px'],
                shadeClose: true,
                btn: ['取消'],
                yes: function (index, layero) {
                    layer.close(index)
                },
            });
        }
    });


    $view.find(".big-img-div img").load(function () {
        commonJS.showImgBig($view, this, this.width, this.height);
    });

    $view.find(".big-img-div").click(function () {//再次点击淡出消失弹出层
        $(this).fadeOut("fast");
    });


    form.on('switch(statusOption)', function (obj) {
        //奖品状态控制点击事件
        var checked = obj.elem.checked;
        var status;
        if (checked) {
            status = "1";
        } else {
            status = "0";
        }
        //获取该控件所在行
        var index = obj.othis.parents('tr').eq(0).data('index');
        //获取表中所有数据 按行取数据及其id
        var id = table.cache['baseGoodsTable'][index].goodsId;

        febs.modal.confirm('更改奖品状态', '确认更改奖品状态', function () {
            updateStatus(id, status);
        }, function () {
            obj.elem.checked = !checked;
            layer.closeAll();
            form.render('checkbox');
        });

    });

    function updateStatus(id, status) {
        //更新奖品状态
        let model = {
            goodsId: id,
            status: status
        }
        febs.post(ctx + 'baseGoods/updateStatus', model, function () {
            febs.alert.success('修改奖品状态成功');
        })
    }

    function getQueryParams() {
        return {
            tenantId: $searchForm.find("select[name='tenantId']").find("option:selected").val(),
            status: $searchForm.find("select[name='status']").find("option:selected").val(),
            goodsName: $searchForm.find('input[name="goodsName"]').val(),
            invalidate_ie_cache: new Date()
        };
    }

    function deleteIds(ids) {
        febs.get(ctx + 'baseGoods/delete/' + ids, null, function () {
            febs.alert.success('删除数据成功');
            $query.trigger('click');
        })
    }

    function getTableHeight() {
        return commonJS.calcTableHeight();
    }

    $(window).resize(function () {
        tableIns.reload({height: getTableHeight()});
    });


});